package com.lancoo.answer.ui.child.training;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lancoo.answer.R;
import com.lancoo.answer.component.OptionComponent;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.util.paperUtils.FragmentCommunicationUtils;
import com.lancoo.answer.widget.audioPlayView.AudioPlayCallBack;

import java.util.List;


/**
 * Created by pengmi on 2021/12/1 20:53
 * Description:小题单选题型作答
 */
public class ChildSelectAnswerFragment extends Fragment {

    private static final String TAG = "QueseSelectAnswerFragme";
    private OptionComponent optionComponent;
    private Child child;
    private long subTime;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.ev_fragment_select_answer_quese, container, false);
    }

    public static Fragment loadChildFragment(int kindIndex, int queseIndex, int smallQueseIndex) {
        ChildSelectAnswerFragment queseFragment = new ChildSelectAnswerFragment();
        Bundle mBundle = new Bundle();
        mBundle.putInt(AnswerConstant.KEY_TYPEINDEX, kindIndex);
        mBundle.putInt(AnswerConstant.KEY_QUESINDEX, queseIndex);
        mBundle.putInt(AnswerConstant.KEY_CHILDINDEX, smallQueseIndex);
        queseFragment.setArguments(mBundle);
        return queseFragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        adjustFontSize();
    }

    private void initView(View view) {
        if (getArguments() == null) {
            return;
        }
        int kindIndex = getArguments().getInt(AnswerConstant.KEY_TYPEINDEX);
        int queseIndex = getArguments().getInt(AnswerConstant.KEY_QUESINDEX);
        int smallQueseIndex = getArguments().getInt(AnswerConstant.KEY_CHILDINDEX);
        Log.e(TAG, "initView，kindIndex：" + kindIndex + ",queseIndex:" + queseIndex + ",smallQueseIndex:" + smallQueseIndex);
        if (ConstantBean.Companion.getConstantExamBean() == null) {
            return;
        }
        List<Type> kindBeanListk = ConstantBean.Companion.getConstantExamBean().getTypeList();
        Ques ques = kindBeanListk.get(kindIndex).getQuesList().get(queseIndex);
        child = kindBeanListk.get(kindIndex).getQuesList().get(queseIndex).getChildList().get(smallQueseIndex);
        optionComponent = view.findViewById(R.id.option_component);
        optionComponent.setChooseColorDeep(true);
        optionComponent.setChild(child);
        optionComponent.setChildIndex(smallQueseIndex);
        optionComponent.setCallback(item -> {
            if (System.currentTimeMillis() - subTime < AnswerConstant.TIME_INTERVAL_DOUBLE_CLICK) {
                return;
            }
            subTime = System.currentTimeMillis();
            Item item1 = child.getItemList().get(0);
            item1.setStuAnswer(item.getOptionIndex());
            FragmentCommunicationUtils.autoNextQuesFragment(ChildSelectAnswerFragment.this, true);

        });
        optionComponent.setPlayCallBack(new AudioPlayCallBack() {
            @Override
            public void onAudioPlay() {
                super.onAudioPlay();
                FragmentCommunicationUtils.closeAllMediaPlay(ChildSelectAnswerFragment.this.getParentFragment());
            }
        });
//        optionComponent.setOptionList(child.getChildOptionList());
        if (TextUtils.equals("k", ques.getTypeNo()) && child.getChildAnswerType() != 0) {
            optionComponent.hideQuesBody();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        pauseChildAudioPlay();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (optionComponent != null) {
            optionComponent.release();
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        changeAnswerStyle();
    }

    /**
     * 暂停小题音频播放
     */
    public void pauseChildAudioPlay() {
        if (optionComponent != null) {
            optionComponent.pause();
        }
    }

    public void changeAnswerStyle() {
        if (optionComponent != null) {
            optionComponent.changeUIStyle();
        }
    }

    /**
     * 调整字体大小
     */
    public void adjustFontSize(){
        View view = getView();
        if(view==null){
            return;
        }
        optionComponent.adjustFontSize();
    }

    public void markQuestion(){
        if(optionComponent==null){
            return;
        }
        optionComponent.markQuestion();
    }
}
